// pages/pay/pay.js
const app = getApp()
const util = require("../../utils/util.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cars: [],
    quan:{ID:0,Money:0},
    productid:0,
    postprice:"包邮",
    postmoney:0,
    orderprice:0,
    payprice:0,
    scoreprice:0,
    addressid:0,
    score:0,
    count:0,
    province:'',
    city:'',
    area:'',
    realname:'',
    mobile: '',
    address: '',
    format:'',
    switchstate:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.from=="car"){
      this.Find_Pay();
    }else{
      this.Find_DefaultAddress(options);
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var score=0;
    if(this.data.switchstate){
      score = this.data.score;
    }
    var payprice = (parseFloat(this.data.orderprice) + parseFloat(this.data.postmoney) - parseFloat(this.data.quan.Money) - score / 100).toFixed(2);
    this.setData({payprice:payprice,quan:this.data.quan});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  selectAddress:function(){
    wx.navigateTo({
      url: '/pages/selectaddress/selectaddress?addressid=' + this.data.addressid
    })
  },
  Find_Pay: function () {
    var that = this;
    var data = {};
    data.act = "Find_Pay";
    wx.showLoading({
      title: '加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      var cars = json.data.carlist;
      var address = json.data.address;
      var quan=json.data.quan;
      var postprices=new Array();
      var orderprice=0;
      var postprice="";
      var payprice=0;
      var postmin=0; //最低商品邮费
      var count=0;
      for(var i=0;i<cars.length;i++){
        postprices.push(cars[i].ExpressMoney);
        orderprice += cars[i].APrice*cars[i].Count;
        count += cars[i].Count;
      }
      postmin = postprices.Min();
      if (postmin==0){
        postprice= "包邮";
      }else{
        postprice = postmin.toFixed(2) + "";
      }
      payprice = orderprice + postmin - quan.Money;
      var scoreprice = app.User.Score / 100;
      if (scoreprice>=payprice)
      {
        scoreprice = payprice;
      }
      /*
      payprice = payprice - scoreprice; //最后执行
      */
      that.setData({ cars: cars, quan: quan, postmoney: postmin, province: address.Province, city: address.City, area: address.Area, realname: address.RealName, mobile: address.Mobile, address: address.Address, addressid: address.ID, postprice: postprice, orderprice: orderprice.toFixed(2), payprice: payprice.toFixed(2), scoreprice: scoreprice.toFixed(2), count: count, score: scoreprice * 100});
    });
  },
  Find_DefaultAddress: function (options) {
    var that = this;
    var data = {};
    data.man = parseFloat(options.aprice) * parseFloat(options.count);
    data.act = "Find_DefaultAddress";
    wx.showLoading({
      title: '加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      var address = json.data.address;
      var quan = json.data.quan;
      var orderprice = parseFloat(options.aprice) * parseFloat(options.count);
      var payprice = orderprice + parseFloat(options.postprice);
      
      var postprice = "";
      if (parseFloat(options.postprice) == "0") {
        postprice = "包邮";
      } else {
        postprice = parseFloat(options.postprice).toFixed(2) + "";
      }
      var product = { Name: options.name, Count: options.count, Img: options.img, Format: options.format.ReplaceAll("@", "&"), APriceText: options.aprice.split('.') };
      that.data.cars.push(product);
      payprice = payprice - quan.Money;
      
      var scoreprice = app.User.Score / 100;
      if (scoreprice >= payprice) {
        scoreprice = payprice;
      }
      /*
      payprice = payprice - scoreprice; //最后执行
      */
      that.setData({ cars: that.data.cars, quan: quan, productid: options.productid, postmoney: options.postprice, postprice: postprice, orderprice: orderprice.toFixed(2), payprice: payprice.toFixed(2), scoreprice: scoreprice.toFixed(2), count: options.count, format: options.format.ReplaceAll("@", "&"), province: address.Province, city: address.City, area: address.Area, realname: address.RealName, mobile: address.Mobile, address: address.Address, addressid: address.ID,score:scoreprice*100 });
    });
  },
  paySuccess:function(e){
    if (this.data.addressid==0){
      app.showError("没有收货地址");
      return;
    }
    var id = e.currentTarget.dataset.id;
    var that = this;
    var data = {};
    data.quanid=this.data.quan.ID;
    data.money=this.data.orderprice;
    data.postmoney = this.data.postmoney;
    data.allmoney = this.data.payprice;
    data.scoremoney=this.data.scoreprice;
    data.quanmoney=this.data.quan.Money;
    data.score = this.data.scoreprice*100;
    data.count=this.data.count;
    data.productid = this.data.productid;
    data.province=this.data.province;
    data.city = this.data.city;
    data.area = this.data.area;
    data.realname = this.data.realname;
    data.mobile = this.data.mobile;
    data.address = this.data.address;
    data.format=this.data.format;
    data.score=this.data.scoreprice*100;
    data.openid=app.User.OpenID;
    data.act = "GetWxParam1";
    wx.showLoading({
      title: '正在调起支付...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      wx.requestPayment({
        'timeStamp': json.data.timestamp,
        'nonceStr': json.data.noncestr,
        'package': json.data.package,
        'signType': 'MD5',
        'paySign': json.data.sign,
        'success': function (res) {
          wx.navigateTo({
            url: '/pages/paysuccess/paysuccess?ordernum=' + json.data.ordernum + "&realname=" + data.realname + "&mobile=" + data.mobile + "&address=" + data.address + "&money=" + data.money
          })
        },
        'fail': function (res) {
        }
      })
    });
    
    
  },
  openCanUseQuan: function(){
    if (this.data.quan.ID>0){
      wx.navigateTo({
        url: '/pages/canusequan/canusequan?man=' + this.data.orderprice
      })
    }
  },
  switchChange:function(e){
    var scoreprice=0;
    var payprice = parseFloat(this.data.orderprice) + parseFloat(this.data.postmoney) - parseFloat(this.data.quan.Money);
    if (e.detail.value){
      scoreprice = this.data.scoreprice;
    }
    else{
      scoreprice = (app.User.Score / 100).toFixed(2);
    }
    
    if (scoreprice >= payprice) {
      scoreprice = payprice;
    } 
    if (e.detail.value)
    {
      payprice = (payprice-scoreprice).toFixed(2);
      this.setData({ switchstate: e.detail.value, scoreprice: scoreprice, payprice: payprice, score: scoreprice*100 });
    }else{
      payprice = (payprice-0).toFixed(2);
      this.setData({ switchstate: e.detail.value, scoreprice: scoreprice, payprice: payprice, score: scoreprice * 100 });
    }

    

  },
  resetScore:function(e){
    var score=e.detail.value;
    this.data.score=score;
    if(score==""){
      score=0;
    }
    if (parseFloat(score) > app.User.Score){
      score = app.User.Score;
      this.setData({ score: score });
      app.showError("可用积分：" + score);
    }
    var scoreprice = (parseFloat(score) / 100);
    var payprice = (parseFloat(this.data.orderprice) + parseFloat(this.data.postmoney) - parseFloat(this.data.quan.Money));
    if (scoreprice >= payprice) {
      scoreprice = payprice;
      this.setData({ score:scoreprice*100});
      app.showError("可用积分：" + scoreprice * 100);
    }
    payprice=payprice-scoreprice;
    this.setData({ payprice: payprice.toFixed(2), scoreprice: scoreprice.toFixed(2) });
  }
})